﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using quanlycuahangtruyen.ketnoi;

namespace quanlycuahangtruyen
{
    public partial class quanlysach : Form
    {
        
        SqlConnection objConnect = new SqlConnection(Dbconnection.getconnectString());

        DataSet objDataset;
        DataView objDataview;

        SqlDataAdapter objDataApdater;
        CurrencyManager current;
        SqlCommand objCommand;
        private void diendl()
        {
            objDataset = new DataSet();

            objDataApdater = new SqlDataAdapter(" select *from truyen ", objConnect);
            objDataApdater.Fill(objDataset, "truyen");
            objDataview = new DataView(objDataset.Tables["truyen"]);




            current = (CurrencyManager)BindingContext[objDataview];
        }
        private void hienthi()
        {
            txtMaSach.DataBindings.Clear();
            txtTenSach.DataBindings.Clear();
            txtTacGia.DataBindings.Clear();
            txtMaNXB.DataBindings.Clear();
            txtMaTL.DataBindings.Clear();
            txtNamXB.DataBindings.Clear();
            txtNgayNhap.DataBindings.Clear();
            txtsotap.DataBindings.Clear();
            txtTomTat.DataBindings.Clear();
            txtghichu.DataBindings.Clear();

            txtMaSach.DataBindings.Add("Text", objDataview, "Matruyen");
            txtMaNXB.DataBindings.Add("Text", objDataview, "MaNXB");
            txtNgayNhap.DataBindings.Add("Text", objDataview, "NgayNhap");
            txtTenSach.DataBindings.Add("Text", objDataview, "TenTruyen");
            txtNamXB.DataBindings.Add("Text", objDataview, "Namxb");
            txtSoLuong.DataBindings.Add("Text", objDataview, "soluong");
            txtTomTat.DataBindings.Add("Text", objDataview, "tomtat");
            txtsotap.DataBindings.Add("Text", objDataview, "sotap");
            txtTacGia.DataBindings.Add("Text", objDataview, "tacgia");
            txtMaTL.DataBindings.Add("Text", objDataview, "Matl");
            txtNgonNgu.DataBindings.Add("Text", objDataview, "NgonNgu");
            txtghichu.DataBindings.Add("Text", objDataview, "ghichu");





         
            
            
        
            
        }
        private void Current()
        {
            textdem2.Text = (current.Position + 1).ToString();
        }

        public quanlysach()
        {
            InitializeComponent();
        }

        private void quanlysach_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'quanLyChoThueTruyenDataSet1.Truyen' table. You can move, or remove it, as needed.
            this.truyenTableAdapter.Fill(this.quanLyChoThueTruyenDataSet1.Truyen);
            diendl();
            hienthi();

        }

        private void back_Click(object sender, EventArgs e)
        {

            {
                current.Position -= 1;
                Current();
            }
        }

        private void backall_Click(object sender, EventArgs e)
        {
            {
                current.Position = 0;
                Current();
            }
            
        }

        private void nextall_Click(object sender, EventArgs e)
        {
            current.Position -= 1;

            current.Position = current.Count - 1;
            Current();

        }

        private void Next_Click(object sender, EventArgs e)
        {
            {
                current.Position += 1;
                Current();
            }

        }

        private void bt_thêm_Click(object sender, EventArgs e)
        {
            if (bt_thêm.Text == "Thêm")
            {
                txtMaNXB.Text = "";
                txtMaSach.Text = "";
                txtMaTL.Text = "";
                txtTacGia.Text = "";
                txtTomTat.Text = "";
                txtNgonNgu.Text = "";
                txtsotap.Text = "";
                txtTenSach.Text = "";
                txtghichu.Text = "";
                txtNamXB.Text = "";
                txtNgayNhap.Text = "";
                txtSoLuong.Text = "";
                bt_thêm.Text = "&Lưu";


            }
            else if (bt_thêm.Text == "Lưu")
            {
                bt_thêm.Text = "&Thêm";
                objCommand = new SqlCommand(" insert into  truyen (  MaTruyen, TenTruyen ,TacGia , Manxb , Namxb,sotap ,NgonNgu,MaTl,tomtat ,soluong,NgayNhap, ghichu )  values (  @MaTruyen, @TenTruyen ,@TacGia , @Manxb , @Namxb,@sotap ,@tomtat,@ngongu , @Matl ,@soluong,@NgayNhap,@ ghichu)", objConnect);
                objCommand.Parameters.AddWithValue("@Matruyen",txtMaSach.Text);
                objCommand.Parameters.AddWithValue("@TenTruyen",txtTenSach.Text);
                objCommand.Parameters.AddWithValue("@Tacgia" ,txtTacGia.Text);
                objCommand.Parameters.AddWithValue("@Manxb" ,txtMaNXB.Text);
                objCommand.Parameters.AddWithValue("@Namxb",txtNamXB.Text);
                objCommand.Parameters.AddWithValue("@sotap" ,txtsotap.Text);
                objCommand.Parameters.AddWithValue("@ngongu ",txtNgonNgu.Text);
                objCommand.Parameters.AddWithValue("@MaTl" ,txtMaTL.Text);
                objCommand.Parameters.AddWithValue("@Tomtat" ,txtTomTat.Text);
                objCommand.Parameters.AddWithValue("@soluong", txtSoLuong.Text);
                objCommand.Parameters.AddWithValue("@NgayNhap ", txtNgayNhap.Text);
                objCommand.Parameters.AddWithValue("@ghichu" , txtghichu.Text);
                objConnect.Open();
                objCommand.ExecuteNonQuery();
                objConnect.Close();
                DataRowView new_row = objDataview.AddNew();
                new_row.BeginEdit();
                new_row["Matruyen"]  = txtMaSach.Text;
                new_row["TenTruyen"]= txtTenSach.Text;
                new_row["Manxb"]=  txtMaNXB.Text;
                new_row["Namxb"] = txtNamXB.Text;
                new_row["sotap"] = txtsotap.Text;
                new_row["ngongu"] = txtNgonNgu.Text;
                new_row["Matl"] = txtMaTL.Text;
                new_row["tomtat"] = txtTomTat.Text;
                new_row["soluong"] = txtSoLuong.Text;
                new_row["Ngaynhap"] = txtNgayNhap.Text;
                new_row[" ghichu"] = txtghichu.Text;
                new_row.EndEdit();


                
            }
        }

        private void bt_sua_Click(object sender, EventArgs e)
        {
            objCommand = new SqlCommand("update Truyen set MaTruyen = @MaTruyen , TenTruyen = @TenTruyen,Tacgia=@Tacgia,Manxb =@Manxb, Namxb= @Namxb,SoTap= @SoTap ,NgonNgu = @NgonNgu, MaTL = @MaTL  , TomTa= @TomTat ,SoLuong=@SoLuong , Ngaynhap= @NgayNhap, ghichu= @ghichu",objConnect);
            objCommand.Parameters.AddWithValue("@Matruyen", txtMaSach.Text);
            objCommand.Parameters.AddWithValue("@TenTruyen", txtTenSach.Text);
            objCommand.Parameters.AddWithValue("@Maxb", txtMaNXB.Text);
            objCommand.Parameters.AddWithValue("@Namxb", txtNamXB.Text);
            objCommand.Parameters.AddWithValue("@SoTap", txtsotap.Text);
            objCommand.Parameters.AddWithValue("@tacgia", txtTacGia.Text);
            objCommand.Parameters.AddWithValue("@Ngonngu", txtNgonNgu.Text);

        }

      
    }
}
